package com.xxxt.zpc.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONObject;
import com.xxxt.entity.Admin;
import com.xxxt.entity.Student;
import com.xxxt.entity.Teacher;
import com.xxxt.utils.MD5Util;
import com.xxxt.zpc.service.CommonService;

@Controller
public class CommonController {

	@Autowired
	private HttpSession session;
	@Autowired
	private CommonService commonService;

	/*
	 * 登录，所有用户都用这个登录方法
	 */
	@PostMapping(value = "login", produces = "application/json;charset=UTF-8")
	@ResponseBody
	public JSONObject login(String userName,String password) {
		JSONObject json=new JSONObject();
		// 状态码
		int status = -1;
		// 提示信息
		String message = "账号或密码不正确";
		// 职位
		String position = null;
		// 名字
		String name = null;
		// 账号
		String account = null;

		if (userName != null && password != null && !userName.equals("") && !password.equals("")) {
			try {
				password=MD5Util.md5withkey(password, "xxxt");
				System.out.println(password);
			} catch (Exception e) {
				e.printStackTrace();
				json.put("status", status);
				json.put("message", message);
				return json;
			}
			Student student = null;
			try {
				student = commonService.studentLogin(userName, password,session);
			} catch (Exception e) {
				e.printStackTrace();
				json.put("status", status);
				json.put("message", message);
				return json;
			}
			if (student != null) {
				// 此次登录是学生登录
				status = 0;
				message = "登陆成功";
				position = "student";
				name = student.getName();
				account= student.getSno();
				json.put("status", status);
				json.put("message", message);
				json.put("position", position);
				json.put("name", name);
				json.put("account", account);
				return json;
			} else {
				Teacher teacher=new Teacher();
				try {
					teacher = commonService.teacherLogin(userName, password,session);
				} catch (Exception e) {
					e.printStackTrace();
					json.put("status", status);
					json.put("message", message);
					return json;
				}
				if (teacher != null) {
					// 此次是老师登录
					if(teacher.getRegisterStatus().equals("agree")){
						status = 0;
						message = "登陆成功";
						position = "teacher";
						name = teacher.getName();
						account = teacher.getTno();
						json.put("status", status);
						json.put("message", message);
						json.put("position", position);
						json.put("name", name);
						json.put("account", account);
						return json;
					}else{
						message = "该账号还没有审核通过";
						json.put("status", status);
						json.put("message", message);
						json.put("position", position);
						json.put("account", account);
						return json;
					}
					
				} else {
					Admin admin;
					try {
						admin = commonService.adminLogin(userName, password,session);
					} catch (Exception e) {
						e.printStackTrace();
						json.put("status", status);
						json.put("message", message);
						return json;
					}
					if (admin != null) {
						// 此次是管理员登录
						status = 0;
						message = "登陆成功";
						position = "admin";
						name = admin.getName();
						account = admin.getAdminId();
						json.put("status", status);
						json.put("message", message);
						json.put("position", position);
						json.put("name", name);
						json.put("account", account);
						return json;
					}
				}
			}
		} else {
			message = "账号密码不能为空";
		}
		json.put("status", 1);
		json.put("message", message);
		return json;
	}

	/*
	 * 退出登录
	 */
	@RequestMapping(value = "loginOut")
	@ResponseBody
	public JSONObject loginOut() {
		JSONObject json=new JSONObject();
		// 状态码
		int status = -1;
		// 提示信息
		String message = "操作失败，请重试！";
		try {
			// 销毁session
			session.invalidate();
			status = 200;
			message = "退出成功";
		} catch (Exception e) {
			e.printStackTrace();
		}
		json.put("status", status);
		json.put("message", message);
		return json;
	}
	
	/*
	 * 修改密码，所有用户修改密码入口
	 */
	@PostMapping(value = "updatePassword")
	@ResponseBody
	public JSONObject updatePassword(String password,String newpassword) {
		JSONObject json=new JSONObject();
		// 状态码
		int status = -1;
		// 提示信息
		String message = "修改密码失败，请重试！";
		
		String identity=(String) session.getAttribute("identity");
		String id=(String) session.getAttribute("account");
		try {
			password=MD5Util.md5withkey(password, "xxxt");
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		if(commonService.selectStudentByIdAndPassword(identity, id, password)>0){
			if(newpassword.length()<6){
				message="密码长度不能少于6";
				status=1;
				json.put("status", status);
				json.put("message", message);
				return json;
			}
			try {
				newpassword=MD5Util.md5withkey(newpassword, "xxxt");
			} catch (Exception e) {
				e.printStackTrace();
				json.put("status", status);
				json.put("message", message);
				return json;
			}
			Boolean result=commonService.updatePasswordById(identity,newpassword, id);
			if(result){
				status=200;
				message="修改成功！";
				session.invalidate();
			}
			json.put("status", status);
			json.put("message", message);
			return json;
		}else{
			status=0;
			message="原密码错误";
			json.put("status", status);
			json.put("message", message);
			return json;
		}
		
	}
}
